package org.example.student_dormitory.service.impl;

import org.example.student_dormitory.dao.StudentDao;
import org.example.student_dormitory.dao.impl.StudentDaoImpl;
import org.example.student_dormitory.dto.Result;
import org.example.student_dormitory.entity.Building;
import org.example.student_dormitory.entity.Dormitory;
import org.example.student_dormitory.entity.Student;
import org.example.student_dormitory.service.StudentService;
import org.example.student_dormitory.utils.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao = new StudentDaoImpl();

    @Override
    public Result loadStudent(String page, String pageSize, String key, String value) {
        //加载分页数据
        System.out.println("2已经进入Service");
        int page_int = Integer.parseInt(page);
        int pageSize_int = Integer.parseInt(pageSize);
        List<Student> list = null;

        //
        System.out.println("3在Service中判断参数类型,准备进入Dao");
        if ("".equals(value) || value == null){
            list = studentDao.selectAll(page_int, pageSize_int);
        }else {
            list = studentDao.selectAll(page_int,pageSize_int,key,value);
        }
        if (list == null || list.size() == 0){
            System.out.println("没数据");
            return new Result<>(100,"没有数据",null);
        }else {
            System.out.println("有数据");
            return new Result<>(200,"有数据",list);
        }
    }

    @Override
    public Result loadBuildings() {
        List<Building> buildings = studentDao.selectBuildings();
        if (buildings == null || buildings.size() == 0){
            return new Result<>(100,"没有数据",null);
        }else {
            return new Result<>(200,"有数据",buildings);
        }
    }

    @Override
    public Result loadDormitory(String buildingId) {
        //加载宿舍信息

        int buildingId_int = Integer.parseInt(buildingId);

        List<Dormitory> list = studentDao.selectByBuildingId(buildingId_int);
        if (list == null || list.size() == 0){

            return new Result<>(100,"没有可入住宿舍",null);
        }else {

            return new Result<>(200,"有可入住宿舍",list);
        }
    }

    @Override
    public Result addStudent(String number, String stuName, String buildingId, String dormitoryId, String gender) {
        Student student = new Student();
//        System.out.println("2已经进入Service");
        student.setNumber(number);
        student.setName(stuName);
//        System.out.println(gender);
        student.setGender(gender);
//        System.out.println(dormitoryId);
        student.setDormitoryId(Long.parseLong(dormitoryId));

        student.setStatus("入住");
        student.setDr(0);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        student.setCreateDate(format);
        //获取数据库连接,事务
        Connection conn = DBUtils.getConn();
        //开启事务手动控制
//        System.out.println("3在Service中判断参数类型,准备进入Dao");
        try {
            conn.setAutoCommit(false);
            //存储学生信息
            boolean insert_stu = studentDao.insertStudent(student, conn);
            boolean update_dormitory = studentDao.updateByDormitoryId(Integer.parseInt(dormitoryId), conn);
            if (insert_stu && update_dormitory){
                //提交事务
                conn.commit();
//                System.out.println("6有数据");
                return new Result<>(200,"添加成功",null);
            }else {
                //回滚
                conn.rollback();
//                System.out.println("6没数据");
                return new Result<>(100,"添加失败",null);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
